<template>
	<view class="wf-box" :style="{ paddingTop: totalheight }">
		<HeadNav titleName="身份认证" :topheight="navBarHeight"></HeadNav>
		<view class="notice" v-if="info&&info.status==3">
			<image src="@/static/img/err.png" mode="widthFix"></image>
			<uni-notice-bar scrollable :speed="speed" :text="info.reason" />
		</view>

		<view class="info info2" v-if="info&&info.status==1">
			<image class="renz_dsh" src="@/static/img/renz_img1.png" mode="widthFix"></image>
			<view class="renz_txt">认证信息已提交，<text>等待审核</text></view>
		</view>
		<view class="info info2" v-if="info&&info.status==2">
			<image class="renz_cg" src="@/static/img/renz_img2.png" mode="widthFix"></image>
			<view class="renz_txt2">恭喜您认证成功!</view>
			<view class="item">
				<view>姓名</view>
				<view>{{info.name}}</view>
				<!-- <input type="text" placeholder="请输入真实姓名" v-model="" /> -->
			</view>
			<view class="item">
				<view>身份证号</view>
				<!-- <input type="text" placeholder="请输入本人身份证号" v-model="" /> -->
				<view>{{mask(info.idCard)}}</view>
			</view>
		</view>

		<view class="info" v-if="!info||info.status==3">
			<view class="title">请手持身份证，并上传身份证的正反面</view>
			<view class="img_bg">
				<image v-if="!img_1" src="@/static/img/aut_img1.png" @click="changeImg(1)" mode="widthFix"></image>
				<image v-else :src="img_1" @click="changeImg(1)" mode="aspectFit" class="img_demo"></image>
			</view>
			<view class="text">上传身份证头像面</view>
			<view class="img_bg">
				<image v-if="!img_2" src="@/static/img/aut_img2.png" @click="changeImg(2)" mode="widthFix"></image>
				<image v-else :src="img_2" @click="changeImg(2)" mode="aspectFit" class="img_demo"></image>
			</view>
			<view class="text">上传身份证国徽面</view>
			<view class="img_bg">
				<image v-if="!img_3" src="@/static/img/aut_img3.png" @click="changeImg(3)" mode="widthFix"></image>
				<image v-else :src="img_3" @click="changeImg(3)" mode="aspectFit" class="img_demo"></image>
			</view>
			<view class="text">上传手持身份证</view>
		</view>
		<view class="info" v-if="!info||info.status==3">
			<view class="title">请完善您的个人信息</view>
			<view class="item">
				<view>姓名</view>
				<input type="text" placeholder="请输入真实姓名" v-model="name" />
			</view>
			<view class="item">
				<view>身份证号</view>
				<input type="text" placeholder="请输入本人身份证号" v-model="card" />
			</view>
		</view>
		<!-- <view v-if="info.is_upload==1&&(info.passed==0||info.passed==1)" class="default_btn btn color_9" @click="hint">
			提交审核</view> -->
		<view v-if="!info" class="default_btn btn" @click="add">提交审核</view>
		<view v-if="info.status==3" class="default_btn btn" @click="edit">提交审核</view>

	</view>
</template>

<script>
	import UNI_APP from '@/.env.js'
	import {
		uploadImage,
		getUserRealName,
		addUserRealName,
		updateUserRealName,
	} from '@/api/api.js'
	import navBarMixin from '@/mixin/navBarMixin.js'
	import HeadNav from "@/components/HeadNav"
	export default {
		data() {
			return {
				img_1: '',
				img_2: '',
				img_3: '',
				userId: '',
				name: '',
				card: '',
				text: '',
				speed: 50,
				info: '',
				type: '',
			}
		},
		mixins: [navBarMixin],
		components: {
			HeadNav
		},
		onLoad(e) {
			console.log(e.type);
			this.type = e.type
			this.select()

		},
		methods: {
			mask(e) {
				return e.replace(/^(.{6})(.*)(.{4})$/, '$1********$3');
			},
			edit() {
				if (!this.img_1 || !this.img_2 || !this.img_3) {
					uni.showToast({
						title: '请上传完整信息',
						icon: 'none'
					})
					return
				}
				updateUserRealName({
					id: this.info.id,
					idCard: this.card,
					idCardAvatar: this.img_1,
					idCardNe: this.img_2,
					name: this.name,
					holdingIdCard: this.img_3
				}).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '提交成功',
							icon: 'none'
						})
						setTimeout(function() {
							uni.navigateBack()
						}, 2000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
					console.log(res);
				})
			},
			add() {
				if (!this.img_1 || !this.img_2 || !this.img_3) {
					uni.showToast({
						title: '请上传完整信息',
						icon: 'none'
					})
					return
				}
				addUserRealName({
					idCard: this.card,
					idCardAvatar: this.img_1,
					idCardNe: this.img_2,
					name: this.name,
					holdingIdCard: this.img_3
				}).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '提交成功',
							icon: 'none'
						})
						setTimeout(function() {
							uni.navigateBack()
						}, 2000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			select() {
				getUserRealName().then(res => {
					console.log(res);
					if (res.code == 0) {
						this.info = res.data?res.data:''
					}
				})
			},
			changeImg(num) {
				console.log(this.userId);
				var that = this
				uni.chooseImage({
					count: 1,
					sizeType: ["compressed"],
					success(response) {
						console.log(response);
						// 选择图片后, 返回的数据
						var fileUrl = response.tempFilePaths[0]
						console.log(fileUrl);
						uploadImage(fileUrl).then(res => {
							console.log(res);
							var info = JSON.parse(res)
							console.log(num);
							if (info.code == 0) {
								if (num == 1) that.img_1 = info.data
								if (num == 2) that.img_2 = info.data
								if (num == 3) that.img_3 = info.data
								console.log(that.img_1);
								// that.save()
							} else {
								uni.showToast({
									title: info.msg,
									icon: 'none',
									duration: 3000
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.wf-box {
		overflow: hidden;
		padding-bottom: 60rpx;
		font-size: 28rpx;
	}

	.info2 {
		text-align: center;
	}

	.info {
		margin: 40rpx 30rpx;
		padding: 40rpx 30rpx 60rpx;
		background-color: white;
		background: #FFFFFF;
		border-radius: 12rpx;
		overflow: hidden;

		.renz_dsh {
			width: 348rpx;
			margin: 160rpx auto 20rpx;
		}
		
		.renz_cg {
			width: 422rpx;
			margin: 170rpx auto 30rpx;
		}

		.renz_txt {
			font-weight: 400;
			font-size: 26rpx;
			margin-bottom: 200rpx;

			text {
				color: #3F93FF;
			}
		}
		.renz_txt2 {
			margin-bottom: 200rpx;
			
		}

		.title {
			font-weight: 500;
			font-size: 32rpx;
		}

		.text {
			text-align: center;
			margin-bottom: 40rpx;
		}

		.img_bg {
			width: 630rpx;
			height: 376rpx;
			background: #F9F9F9;
			border-radius: 12rpx;
			margin: 30rpx 0;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 401rpx;
			}

			.img_demo {
				width: 401rpx !important;
				height: 283rpx !important;
				margin: 0 !important;
				// position: absolute;
				// top: 0;
				// left: 0;
			}
		}

		.item {
			height: 110rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #EEEEEE;

			input {
				font-size: 26rpx;
				text-align: right;
			}
		}
	}

	.btn {
		margin: 80rpx auto 100rpx;
	}

	.color_9 {
		background: #999999;
	}

	/deep/ .uni-noticebar {
		width: 690rpx;
		background-color: #FFF3EE;
		border-radius: 59rpx;
		padding-left: 80rpx;
	}

	.notice {
		margin: 40rpx 30rpx;
		position: relative;

		image {
			position: absolute;
			left: 20rpx;
			top: 12rpx;
			width: 48rpx;
		}
	}

	.default_btn {
		width: 460rpx;
		height: 80rpx;
		background: #3F93FF;
		text-align: center;
		border-radius: 40rpx 40rpx 40rpx 40rpx;
		font-weight: 500;
		font-size: 32rpx;
		color: #FFFFFF;
		line-height: 80rpx;
	}
</style>